package sy.service.impl;

import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Service;
import sy.pageModel.DataGrid;
import sy.pageModel.Tourism;
import sy.service.TourismServiceI;
import sy.util.StringUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016-07-21.
 */
@Service
public class TourismServiceImpl extends BaseService implements TourismServiceI {

    @Override
    public DataGrid findMassageList(Tourism curr) throws Exception {
        DataGrid dg = new DataGrid();
        String sql = "SELECT count(1) FROM xj_tourism t where 1=1";
        sql = addWhere(curr, sql);
        long total = template.queryForLong(sql, new Object[]{});
        System.out.println(total);
        dg.setTotal(total);
        sql = "select t.*,p.name from xj_tourism t,p_info p where t.deptid=p.id ";
        sql = addWhere(curr, sql);
        sql = addOrderBy(curr, sql);
        sql = addLimit(curr, sql);
        System.out.println(sql);
        final List<Tourism> currList = new ArrayList<Tourism>();
        template.query(sql, new Object[]{}, new RowCallbackHandler(){
            public void processRow(ResultSet rs) throws SQLException {
                Tourism t = new Tourism();
                t.setId(rs.getString("t.id"));
                t.setName(rs.getString("t.circuit"));
                t.setPrice(rs.getString("t.price"));
                t.setOrder(rs.getString("t.orderid"));
                t.setDeptId(rs.getString("t.deptid"));
                t.setDeptName(rs.getString("p.name"));
                t.setDetail(rs.getString("t.detail"));
                t.setAttention(rs.getString("t.attention"));
                currList.add(t);
            }});
        dg.setRows(currList);
        return dg;
    }

    @Override
    public Tourism editTourism(Tourism curr) {
        String sql="UPDATE xj_tourism t SET t.circuit=?,t.price=?,t.orderid=?,t.deptid=?,t.detail=?,t.attention=?  WHERE t.id=?";
        template.update(sql,new Object[]{curr.getName(),curr.getPrice(),curr.getOrder(),curr.getDeptId(),curr.getDetail(),curr.getAttention(),curr.getId()});
        return curr;
    }

    @Override
    public Tourism addTourism(Tourism curr) throws Exception {
        String sql="INSERT INTO xj_tourism ( circuit, price, orderid, deptid, detail, attention) VALUES (?,?,?,?,?,?)";
        template.update(sql,new Object[]{curr.getName(),curr.getPrice(),curr.getOrder(),curr.getDeptId(),curr.getDetail(),curr.getAttention()});
        return curr;
    }

    @Override
    public Tourism deleteTourism(Tourism curr) throws Exception {
        String sql="DELETE FROM xj_tourism where id=?";
        String ids1[]=curr.getIds().split(",");
        for (int i = 0; i <ids1.length ; i++) {
            template.update(sql,new Object[]{ids1[i]});
        }
        return curr;
    }

    private String addWhere(Tourism curr, String sql) {
        if(!StringUtil.isNullOrEmpty(curr.getName())) {
            sql += " and t.name like '%" + curr.getName() + "%'";
        }
        if(!StringUtil.isNullOrEmpty(curr.getDeptId())) {
            sql += " and t.deptid = '" + curr.getDeptId() + "'";
        }
        return sql;
    }

    private String addLimit(Tourism curr, String sql) {
        int fromRow = (curr.getPage() - 1) * curr.getRows();
        sql += " limit " + fromRow + "," + curr.getRows();
        return sql;
    }

    private String addOrderBy(Tourism curr, String sql) {
        if (curr.getSort() != null) {
            if("name".equals(curr.getSort())) {
                curr.setSort("t.circuit");
            }
            if("deptName".equals(curr.getSort())) {
                curr.setSort("t.deptid");
            }
            if("order".equals(curr.getSort())) {
                curr.setSort("t.orderid");
            }
            if("price".equals(curr.getSort())) {
                curr.setSort("t.price");
            }
            sql += " order by " + curr.getSort() + " " + curr.getOrder();
        }
        return sql;
    }


}
